const db = require('./config/database');

async function checkDatabaseStructure() {
  try {
    console.log('检查数据库表结构...');

    // 检查表是否存在
    const tables = await db.query('SHOW TABLES');
    console.log('数据库中的表:', tables.map(t => Object.values(t)[0]));

    // 检查线索表结构
    if (tables.some(t => Object.values(t)[0] === 'clues')) {
      console.log('\n=== clues 表结构 ===');
      const cluesStructure = await db.query('DESCRIBE clues');
      cluesStructure.forEach(col => {
        console.log(`${col.Field}: ${col.Type} ${col.Null === 'NO' ? 'NOT NULL' : 'NULL'} ${col.Default ? `DEFAULT ${col.Default}` : ''}`);
      });
    }

    // 检查管理员表结构
    if (tables.some(t => Object.values(t)[0] === 'admins')) {
      console.log('\n=== admins 表结构 ===');
      const adminsStructure = await db.query('DESCRIBE admins');
      adminsStructure.forEach(col => {
        console.log(`${col.Field}: ${col.Type} ${col.Null === 'NO' ? 'NOT NULL' : 'NULL'} ${col.Default ? `DEFAULT ${col.Default}` : ''}`);
      });
    }

    // 检查处理日志表结构
    if (tables.some(t => Object.values(t)[0] === 'clue_processing_logs')) {
      console.log('\n=== clue_processing_logs 表结构 ===');
      const logsStructure = await db.query('DESCRIBE clue_processing_logs');
      logsStructure.forEach(col => {
        console.log(`${col.Field}: ${col.Type} ${col.Null === 'NO' ? 'NOT NULL' : 'NULL'} ${col.Default ? `DEFAULT ${col.Default}` : ''}`);
      });
    }

    // 检查现有数据
    console.log('\n=== 现有数据统计 ===');
    
    if (tables.some(t => Object.values(t)[0] === 'admins')) {
      const adminCount = await db.query('SELECT COUNT(*) as count FROM admins');
      console.log(`管理员数量: ${adminCount[0].count}`);
      
      if (adminCount[0].count > 0) {
        const admins = await db.query('SELECT username, real_name, role FROM admins');
        console.log('管理员列表:', admins);
      }
    }

    if (tables.some(t => Object.values(t)[0] === 'clues')) {
      const clueCount = await db.query('SELECT COUNT(*) as count FROM clues');
      console.log(`线索数量: ${clueCount[0].count}`);
      
      if (clueCount[0].count > 0) {
        const clues = await db.query('SELECT id, clue_no, title, status FROM clues LIMIT 5');
        console.log('线索列表（前5条）:', clues);
      }
    }

  } catch (error) {
    console.error('检查数据库结构失败:', error);
  } finally {
    process.exit(0);
  }
}

checkDatabaseStructure(); 